<template>
  <div class="tree-box">
    <el-tree
      ref="tree"
      :data="treeData"
      node-key="id"
      :props="defaultProps"
      :default-expanded-keys="expandArr"
      :expand-on-click-node="false"
    >
    <span class="custom-tree-node" slot-scope="{ node, data }">
      <span>{{ node.data.code }}</span>
      <div style="width: 70%">
        <span style="text-align: left">{{ node.label }}</span>
      </div>
      <el-button type="text" size="mini" @click="() => appendNode(node,data)">添加</el-button>
      <el-button type="text" size="mini" @click="() => editNode(node,data)">修改</el-button>
      <el-button type="text"  size="mini" @click="() => remove(data)">删除</el-button>
    </span>
    </el-tree>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleCancle">
      <el-form ref="form" :model="addForm" :rules="rules" label-width="50px" style="padding: 0 20px">
        <el-form-item label="编码" prop="code" style="margin-bottom: 22px">
          <el-input v-model="addForm.code" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="描述" prop="value" style="margin-bottom: 22px">
          <el-input v-model="addForm.value" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="margin-bottom: 22px">
          <el-input v-model="addForm.remark" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAccountType, addAccountType, delRegisterType, editRegisterType } from '@/api/modules/systemManage.js'
export default {
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'value',
        children: 'child'
      },
      expandArr: ['root'],
      dialogVisible: false,
      addForm: {
        code: '',
        value: '',
        remark: '',
        parentId: '',
        level: ''
      },
      rules: {
        code: [{ required: true, message: '请输入编码', trigger: 'change' }],
        value: [{ required: true, message: '请输入描述', trigger: 'change' }]
      },
      title: '',
      disabled: false
    }   
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      getAccountType().then(res => {
        if (res.data.code === '200') {
          this.treeData = res.data.data
        }
      })
    },
    appendNode(data) {
      this.dialogVisible = true
      this.title = '新增'
      this.disabled = false
      this.addForm.parentId = data.data.id
      this.addForm.level = data.data.level
      this.expandArr = [data.data.id]
    },
    editNode(data) {
      console.log(data)
      this.dialogVisible = true
      this.title = '修改'
      this.disabled = true
      this.addForm = data.data
      this.expandArr = [data.data.id]
    },
    handleSubmit(formName) {
      if (this.title === '新增') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addAccountType(this.addForm).then(res => {
              if (res.data.code === '200') {
                this.$message.success('新增成功')
                this.handleCancle()
                this.initData()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      } else if (this.title === '修改') {
        editRegisterType(this.addForm).then(res => {
          if (res.data.code === '200') {
            this.$message.success('修改成功')
            this.dialogVisible = false
            this.initData()
          }
        })
      }
    },
    remove(data) {
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRegisterType(data).then(res => {
          if (res.data.code === '200') {
            this.$message.success('删除成功')
            this.initData()
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })     
      })
    },
    handleCancle() {
      this.resetForm('form')
      this.dialogVisible = false
      this.initData()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
  .tree-box {
    padding: 30px;
    height: 80vh;
    overflow: auto;
    .custom-tree-node {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
    }
  }
</style>
<style>
  .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
  }
  .el-input.is-disabled .el-input__inner {
    background-color:rgb(228,231,237,0.15);
    border-color: #E4E7ED;
    color: #606266;
  }
  .el-textarea.is-disabled .el-textarea__inner {
    background-color: rgb(228,231,237,0.15);
    border-color: #E4E7ED;
    color: #606266;
  }
</style>